//
//  ForePWViewController.m
//  iOS-CtrlV
//
//  Created by Wang Li on 12-12-19.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "ForePWViewController.h"
#import "stringAddition.h"
#import "ASIFormDataRequest.h"
#import "CJSONDeserializer.h"
@interface ForePWViewController ()

@end

@implementation ForePWViewController

- (id)init
{
    if (self = [super init]) {
        // Custom initialization
    }
    return self;
}

-(void) dealloc
{
    [_InputField release];
    [_BackBttBarItem release];
    [_ApplyBttBarItem release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor colorWithRed:193/255.f green:193/255.f blue:193/255.f alpha:1]];
    _BackBttBarItem =[UIButton buttonWithType:UIButtonTypeCustom];
    _BackBttBarItem.frame =CGRectMake(0, 0, 44, 44);
    [_BackBttBarItem setImage:[UIImage imageNamed:@"navclose.png"] forState:UIControlStateNormal];
    [_BackBttBarItem addTarget:self action:@selector(navBackBtt) forControlEvents:UIControlEventTouchUpInside];
    [_BackBttBarItem retain];
    [self.navigationController.navigationBar addSubview:_BackBttBarItem];
    
    _ApplyBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    _ApplyBttBarItem.frame =CGRectMake(320-44, 0, 44, 44);
    [_ApplyBttBarItem setImage:[UIImage imageNamed:@"navyes.png"] forState:UIControlStateNormal];
    [_ApplyBttBarItem addTarget:self action:@selector(applyGetPW) forControlEvents:UIControlEventTouchUpInside];
    [_ApplyBttBarItem retain];
    [self.navigationController.navigationBar addSubview:_ApplyBttBarItem];
    
    UILabel* tTipLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 300, 30)];
    [tTipLab setBackgroundColor:[UIColor clearColor]];
    tTipLab.text =@"输入邮箱地址";
    [tTipLab setTextColor:[UIColor darkGrayColor]];
    [self.view addSubview:tTipLab];
    [tTipLab release];
    
    _InputField = [[UITextField alloc] initWithFrame:CGRectMake(10, 44, 300, 100)];
    _InputField.borderStyle =UITextBorderStyleRoundedRect;
    [self.view addSubview:_InputField];

}


-(void) viewWillDisappear:(BOOL)animated
{
    [_BackBttBarItem removeFromSuperview];
    [_ApplyBttBarItem removeFromSuperview];
}

-(void) viewWillAppear:(BOOL)animated
{
    if ([_BackBttBarItem superview] ==nil) {
        [self.navigationController.navigationBar addSubview:_BackBttBarItem];
    }
    if ([_ApplyBttBarItem superview] ==nil) {
        [self.navigationController.navigationBar addSubview:_ApplyBttBarItem];
    }
}


-(void) navBackBtt
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) applyGetPW
{
    NSString* tTipStr =nil;
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if ([_InputField.text length] > 0) {
        if ([_InputField.text isEmailFormat]) {
            
            ASIFormDataRequest* tPostRequest =[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?app=userNew&act=forgetPwd",RequestMainURL]]];
            [tPostRequest setDelegate:self];
            [tPostRequest setPostValue:_InputField.text forKey:@"email"];
            [tPostRequest setDidFinishSelector:@selector(getPWFinish:)];
            [tPostRequest startAsynchronous];
            [self addHudLoad];
        }else{
            tTipStr =@"请输入正确的邮件格式";
            [tAppDel showWithCustomView:@"提示" detailText:tTipStr isCue:NO delayTime:3.0f];
        }
    }else{
        tTipStr =@"请输邮件地址";
    }
    if ([tTipStr length] > 1) {
        [tAppDel showWithCustomView:@"提示" detailText:tTipStr isCue:NO delayTime:3.0f];
    }
}


- (void)requestFailed:(ASIHTTPRequest*)request
{
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:@"网络错误请稍后再试" isCue:YES delayTime:3.0f];
}
-(void)getPWFinish:(ASIHTTPRequest*)request
{
    NSString* tStrReq =[request responseString];
    NSLog(@"%@",tStrReq);
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    if ([tStrReq length] > 0) {
        NSError* tError;
        NSData *jsonData = [tStrReq dataUsingEncoding:NSUTF32BigEndianStringEncoding];
        NSDictionary *tDictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&tError];
        int tErrorCode= [[tDictionary objectForKey:@"errorCode"] intValue];
        if (tErrorCode == 0) {
            [tAppDel showWithCustomView:@"提示" detailText:@"取回密码成功" isCue:NO delayTime:2.0f];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [tAppDel showWithCustomView:@"提示" detailText:[tDictionary objectForKey:@"errorMsg"] isCue:YES delayTime:3.0f];
        }
    }else{
        [tAppDel showWithCustomView:@"提示" detailText:@"获得数据错误" isCue:YES delayTime:3.0f];
    }
    [self hudWasHidden:nil];
}


#pragma mark -
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    _MBHUD.delegate = nil;
	[_MBHUD removeFromSuperview];
	[_MBHUD release];
	_MBHUD=nil;
}

-(void) addHudLoad{
	if (_MBHUD == nil) {
		_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
		[self.navigationController.view addSubview:_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		_MBHUD.delegate = self;
		_MBHUD.labelText = @"请求数据中...";
		[_MBHUD show:YES];
	}
}
@end
